package com.woyou.controller;

import java.io.File;

import com.citaq.ideliver.R;
import com.squareup.picasso.Picasso;
import com.woyou.bean.Info;
import com.woyou.model.ActivitiesModel;
import com.woyou.model.ActivitiesModel_;
import com.woyou.model.FileModel;
import com.woyou.model.FileModel_;
import com.woyou.model.GoodsModel;
import com.woyou.model.GoodsModel_;
import com.woyou.model.OrderModel;
import com.woyou.model.OrderModel_;
import com.woyou.model.PropPanelModel;
import com.woyou.model.PropPanelModel_;
import com.woyou.model.ShoppingCarModel;
import com.woyou.model.ShoppingCarModel_;
import com.woyou.model.ShopsModel;
import com.woyou.model.ShopsModel_;
import com.woyou.model.UserModel;
import com.woyou.model.UserModel_;
import com.woyou.service.NetWorkCenter;
import com.woyou.service.NetWorkCenter_;
import com.woyou.service.ThreadPoolManager;
import com.woyou.service.ThreadPoolManager_;
import com.woyou.service.location.BaiduLocationService;
import com.woyou.service.location.BaiduLocationService_;
import com.woyou.ui.api.IBackEventStrategy;
import com.woyou.ui.view.SuperUI;
import com.woyou.utils.Task;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.Toast;


/**
 * 所有控制器的超类
 * 
 * @author longtao.li
 * 
 */
public abstract class SuperController implements IBackEventStrategy, OnClickListener {
	
	protected Context mContext;
	
	protected Activity mActivity;
	
	protected SuperUI mSuperUI;

	protected ProgressDialog progress;

	@SuppressWarnings("rawtypes")
	protected Info mInfo;

	protected final String INFO_NAME = "info";
	
	protected NetWorkCenter mNetWorkCenter;
	
	protected BaiduLocationService mBaiduLocationService;
	
	ThreadPoolManager mThreadPoolManager;
	
	/**所有Model**/
	protected ShopsModel mShopsModel;
	protected ShoppingCarModel mShoppingCarModel;
	protected GoodsModel mGoodsModel;
	protected ActivitiesModel mActivitiesModel;
	protected FileModel mFileModel;
	protected PropPanelModel mPropPanelModel;
	protected OrderModel mOrderModel;
	protected UserModel mUserModel;

	/**
	 * 当前最后执行的线程任务,task的ID属性可以用于判断线程启动的先后
	 */
	protected Task lastTask = new Task(0) {

		@Override
		public void run() {

		}
	};
	
	public SuperController(Context context, SuperUI superUI) {
		mContext = context;
		mActivity = (Activity) mContext;
		
		mSuperUI = superUI;
		
		mThreadPoolManager = ThreadPoolManager_.getInstance_(mContext);
		
		mNetWorkCenter = NetWorkCenter_.getInstance_(mContext.getApplicationContext());
		
		mBaiduLocationService = BaiduLocationService_.getInstance_(mContext.getApplicationContext());

		/*model实例化*/
		mShopsModel = ShopsModel_.getInstance_(context);
		mShoppingCarModel = ShoppingCarModel_.getInstance_(context);
		mGoodsModel = GoodsModel_.getInstance_(context);
		mActivitiesModel = ActivitiesModel_.getInstance_(context);
		mFileModel = FileModel_.getInstance_(context);
		mPropPanelModel = PropPanelModel_.getInstance_(context);
		mOrderModel = OrderModel_.getInstance_(context);
		mUserModel = UserModel_.getInstance_(context);
		
		progress = getProgressDialog("正在加载,请稍后...");
		progress.setCancelable(true);
		
	}


	public ProgressDialog getProgressDialog(String msg) {
		ProgressDialog progressDialog = new ProgressDialog(mContext);
		progressDialog.setMessage(msg);
		progressDialog.setCancelable(true);
		return progressDialog;
	}

	public void showToastOnUI(final String msg) {
		runOnUI(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
			}
		});

	}
	
	public void showToast(String msg) {
		Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * UI线程执行一个任务
	 * 
	 * @param run
	 */
	public void runOnUI(Runnable run) {
		mActivity.runOnUiThread(run);
	}

	/**
	 * 子线程执行一个任务
	 * 
	 * @param task
	 */
	public void executeTask(Task task) {
		this.lastTask = task;
		mThreadPoolManager.executeTask(task);
	}

	/**
	 * 
	 * @param run
	 */
	public void executeTask(Runnable run) {
		mThreadPoolManager.executeTask(run);
	}

	/**
	 * 隐藏输入法
	 * 
	 * @param context
	 * @param achor
	 */
	public  void hideSoftInput(Context context, View achor) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(achor.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	public void dismissProgressDialog() {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				if (progress != null) {
					progress.dismiss();
				}

			}
		});

	}

	public void showProgressDialog() {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				if (progress == null) {
					progress = new ProgressDialog(mContext);
					progress.setMessage("正在加载,请稍后...");
					progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				}
				progress.setCancelable(true);
				try {
					progress.show();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

	}
	
	public void showProgressDialog(final String msg, final boolean isCancel) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				progress = new ProgressDialog(mContext);
				progress.setMessage(msg);
				progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				progress.setCancelable(isCancel);
				try {
					progress.show();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

	}



	/**
	 * 打开一个Activity
	 * 
	 * @param clazz
	 * @param info
	 */
	@SuppressWarnings("rawtypes")
	public void openActivity(final Class clazz, final Info info) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				Intent intent = new Intent();
				intent.setClass(mContext, clazz);
				if (info != null) {
					intent.putExtra(INFO_NAME, info);
				}
				mContext.startActivity(intent);
			}
		});

	}

	/**
	 * 打开一个Activity for result
	 * 
	 * @param clazz
	 * @param info
	 */
	@SuppressWarnings("rawtypes")
	public void openActivity4Result(final Class clazz, final Info info,
			final int requestCode) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				Intent intent = new Intent();
				intent.setClass(mContext, clazz);
				if (info != null) {
					intent.putExtra(INFO_NAME, info);
				}
				mActivity.startActivityForResult(intent, requestCode);
			}
		});

	}

	/**
	 * 返回数据到上一个activity
	 * 
	 * @param resultCode
	 * @param data
	 */
	@SuppressWarnings("rawtypes")
	public void closeActivity4Result(int resultCode, Info data) {
		Intent intent = mActivity.getIntent();
		intent.putExtra(INFO_NAME, data);
		mActivity.setResult(resultCode, intent);
		mActivity.finish();
	}

	/**
	 * 加载图片
	 * 
	 * @param tuContainer
	 * @param item
	 */
	public void loadIMG(ImageView img, String url) {
		Picasso.with(mContext).load(url).error(R.drawable.error).into(img);
	}
	
	public void loadIMG(ImageView img, String url, int errorId) {
		if( errorId == 0 ){
			Picasso.with(mContext).load(url).into(img);
		}else{
			Picasso.with(mContext).load(url).error(errorId).into(img);
		}
		
	}
	
	public void loadIMG(ImageView img, String url, int placeholder, int errorId) {
		Picasso.with(mContext).load(url).error(errorId)
				.placeholder(placeholder).into(img);
	}

	/**
	 * 加载图片
	 * 
	 * @param tuContainer
	 * @param item
	 */
	public void loadIMG(ImageView img, File file) {
		Picasso.with(mContext).load(file).error(R.drawable.error).into(img);
	}

	/**
	 * 加载本地图片
	 * 
	 * @param tuContainer
	 * @param item
	 */
	public void loadIMG(ImageView img, int id) {
		Picasso.with(mContext).load(id).into(img);
	}

	/**
	 * 获得配置文件参数
	 * 
	 * @param id
	 * @return
	 */
	public float getDimens(int id) {
		return mContext.getResources().getDimension(id);
	}

	
	@Override
	public void onClick(View v) {
		
	}
	
	/**
	 * 获取静态地图的url
	 * @return
	 */
	public String getStaticMapUrl(String w, String h, String center, String markers, String zoom){
		String url = "http://api.map.baidu.com/staticimage?width=%s&height=%s&center=%s&markers=%s&zoom=%s";
		url = String.format(url, w, h, center, markers, zoom);
		return url;
	}
	

}
